# Palindrome.py # # Description: Calls the Palindrome function (from Practice Exam #4 Q.9) # and prints # That’s a palindrome! # when the function returns True and # That’s no palindrome! # when then function returns False. # # The program terminates when the user enters an empty string. # # Author: Anne Lavergne # Date: Feb. 28 2024 def palindrome(aWord): """Returns True if 'aWord' is a palindrome and False it is not.""" # The idea of this algorihtm is: # Imagine we are folding aWord in half and checking # whether each pair of matching letters are identical # If so, we have a palindrome # Get the positive index of the last (rightmost) letter of aWord reverseIndex = len(aWord)-1 # Let's compare each letter of the leftmost half of aWord with # its matching counterpart letter in the rightmost half of aWord for anIndex in range(0,len(aWord)//2): # If the letters in the current pair are not identical # then aWord is not a palindrome if aWord[anIndex] != aWord[reverseIndex]: return False # Go to the next pair of letters reverseIndex -= 1 # If we reach this point, aWord is a palindrome return True # ***Main part of my program theWord = input("Enter a word to check: ") while theWord: if palindrome(theWord): print("That's a palindrome!") else: print("That's no palindrome!") theWord = input("Enter a word to check: ")